//
// Created by dqchen on 2022/11/2.
//

#include "scanner.h"

Scanner::Scanner(FILE *f) {
    file = f;
}

Scanner::~Scanner() {

}

char Scanner::scan() {
    if (!file) {
        return -1;
    }
    if(readPos == lineLen - 1){
        lineLen = fread(line, 1, BUFLEN, file);
        if (lineLen == 0) {
            lineLen = 1;
            line[0] = -1;
        }
        readPos = -1;
    }
    readPos ++;
    char ch = line[readPos];
    if (last == '\n') {
        lineNum ++;
        colNum = 0;
    }
    if (ch == -1) {
        fclose(file);
        file = NULL;
    } else{
        colNum ++;
    }
    last = ch;
    return ch;
}
